﻿using UnityEngine;
public delegate void OnStart();
public delegate void OnSecond(float _Value);
public delegate void OnLoopComplete(int count);
public delegate void OnlineAnalyzePanel();
public delegate void Remove(long idx);
public class TimerData
{
    ~TimerData() { }
    public long Idx;
    public float _duration = 0f;
    public float _delayTime = 0f;
    public bool _delayForLoop = false;
    public int _loopTime = 0;
    public TimingType _timingType = TimingType.ACCUMULATION;
    public float _currTime = 0f;
    private float _second = 0f;
    private float _delay = 0f;
    private bool isLoop = false;
    public OnStart _actionStart = null;
    public OnSecond _actionPerSecond = null;
    public OnLoopComplete _actionLoopComplete = null;
    public OnlineAnalyzePanel _actionComplete = null;
    public Remove _Remove = null;
    public bool isComplete = false;

    public TimerData(float duration, long idx, Remove Remove)
    {
        _duration = duration;
        Idx = idx;
        _Remove = Remove;
    }
    /// <summary>
    /// 设置启动延时
    /// </summary>
    /// <param name="delay"></param>
    /// <returns></returns>
    public TimerData SetDalay(float delay)
    {
        this._delayTime = delay;
        this._delay = _delayTime;
        return this;
    }
    /// <summary>
    /// 设置循环次数
    /// </summary>
    /// <param name="delayForLoop"></param>
    /// <param name="loopTime"></param>
    /// <returns></returns>
    public TimerData SetLoop(bool delayForLoop, int loopTime)
    {
        this._delayForLoop = delayForLoop;
        this._loopTime = loopTime;
        this.isLoop = (_loopTime > 0 || _loopTime == -1);
        return this;
    }
    /// <summary>
    /// 设置计时类型
    /// </summary>
    /// <param name="timingType"></param>
    /// <returns></returns>
    public TimerData SetType(TimingType timingType)
    {
        this._timingType = timingType;
        if (_timingType == TimingType.ACCUMULATION)
        {
            _currTime = 0f;
        }
        else if (_timingType == TimingType.COUNTDOWN)
        {
            _currTime = _duration;
        }
        return this;
    }

    /// <summary>
    /// 开始调用
    /// </summary>
    /// <param name="actionStart"></param>
    /// <returns></returns>
    public TimerData OnStart(OnStart actionStart)
    {
        this._actionStart = actionStart;
        return this;
    }
    /// <summary>
    /// 每一秒调用
    /// </summary>
    /// <param name="actionPerSecond"></param>
    /// <returns></returns>
    public TimerData OnSecond(OnSecond actionPerSecond)
    {
        this._actionPerSecond = actionPerSecond;
        return this;
    }
    /// <summary>
    /// 每次循环结束调用
    /// </summary>
    /// <param name="actionLoopComplete"></param>
    /// <returns></returns>
    public TimerData OnLoopComplete(OnLoopComplete actionLoopComplete)
    {
        this._actionLoopComplete = actionLoopComplete;
        return this;
    }
    /// <summary>
    /// 结束调用
    /// </summary>
    /// <param name="actionComplete"></param>
    /// <returns></returns>
    public TimerData OnComplete(OnlineAnalyzePanel actionComplete)
    {
        this._actionComplete = actionComplete;
        return this;
    }
    /// <summary>
    /// 计时器结束
    /// </summary>
    /// <returns></returns>
    public long End()
    {
        return this.Idx;
    }

    private bool isStart = false;
    public void SetUpdate()
    {
        if (isComplete)
        {
            return;
        }
        float deltaTime = Time.deltaTime;
        if (_delay > 0)
        {
            _delay -= deltaTime;
            return;
        }
        if (!isStart)
        {
            isStart = true;
            _actionStart?.Invoke();
        }
        _second += deltaTime;
        if (_second >= 1)
        {
            _actionPerSecond?.Invoke(_currTime);
            _second -= 1;
        }
        if (_timingType == TimingType.ACCUMULATION)
        {
            if (_duration != -1 && _currTime >= _duration) //到了
            {
                if (isLoop)
                {
                    if (_loopTime > 0 || _loopTime == -1)
                    {
                        _actionLoopComplete?.Invoke(_loopTime);
                        if (_delayForLoop)
                        {
                            _delay = _delayTime;
                        }
                        _currTime = 0;
                        if (_loopTime > 0)
                        {
                            _loopTime--;
                        }
                        _second = 0;
                    }
                    else
                    {
                        _Remove?.Invoke(Idx);
                        _actionComplete?.Invoke();
                    }
                }
                else
                {
                    _Remove?.Invoke(Idx);
                    _actionComplete?.Invoke();
                }
                return;
            }
            else
            {
                _currTime += deltaTime;
            }
        }
        else if (_timingType == TimingType.COUNTDOWN)
        {
            if (_currTime <= 0)//到了
            {
                if (isLoop)
                {
                    if (_loopTime > 0 || _loopTime == -1)
                    {
                        _actionLoopComplete?.Invoke(_loopTime);
                        if (_delayForLoop)
                        {
                            _delay = _delayTime;
                        }
                        _currTime = _duration;
                        if (_loopTime > 0)
                        {
                            _loopTime--;
                        }
                        _second = 0;

                    }
                    else
                    {

                        _Remove?.Invoke(Idx);
                        _actionComplete?.Invoke();
                    }

                }
                else
                {
                    _Remove?.Invoke(Idx);
                    _actionComplete?.Invoke();
                }
                return;
            }
            else
            {
                _currTime -= deltaTime;
            }
        }

    }
}


public enum TimingType
{
    //倒计时
    COUNTDOWN,
    //正计时
    ACCUMULATION,
}